<?php
    $this->beginWidget('zii.widgets.jui.CJuiDialog',array(
                            'id'=>'fileDialog',
                            'options'=>array(
                            'title'=>'File',
                            'autoOpen'=>true,
                            'modal'=>true,
                            'width'=>970,
                            'height'=>515,
                            'resizable' => false,
                            ),
                    ));
    $this->renderPartial('/media/_browser');
    echo '<table class="form" style="width: 100%">';
    echo'<tr>
            <td colspan="2" class="cbutton">';
    echo CHtml::htmlButton('Insert',array('id'=>'btnInsert','class'=>'button'));     
    echo CHtml::htmlButton('Cancel',array('id'=>'btnCancel','onclick'=>'$("#fileDialog").dialog("close");','class'=>'button'));
    echo'   </td>
        </tr>';
    echo'</table> ';
    $this->endWidget('zii.widgets.jui.CJuiDialog');
?>

<script type="text/javascript">
function getElementsByClassName(node,classname) {
        if (node.getElementsByClassName) { // use native implementation if available
            return node.getElementsByClassName(classname);
        } else {
            return (function getElementsByClass(searchClass,node) {
                if ( node == null )
                node = document;
                var classElements = [],
                    els = node.getElementsByTagName("*"),
                    elsLen = els.length,
                    pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

                for (i = 0, j = 0; i < elsLen; i++) {
                    if ( pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                    }
                }
                return classElements;
            })(classname, node);
        }
    }
$(document).ready(function () {     
    $('#btnInsert').click(function(){
        <?php 
            $arrConfig = $this->mediaConfig;
            $serverUrl = MEDIA_FOLDER_NAME .'/';
        ?>        
        fileName = getElementsByClassName(document, 'elfinder-cwd-filename ui-draggable ui-state-hover');        
        filePathNodes = getElementsByClassName(document, 'elfinder-path');
        filePath = filePathNodes[0].getElementsByTagName('a');
        if(fileName.length == 0){
            alert("<?php echo t('MSG_ERROR_NO_FILE_CHOOSE', 'media');?>");
        }
        else if(fileName.length > 1){
            alert("<?php echo t('MSG_ERROR_MULTI_FILE_CHOOSE', 'media');?>");
        }
        else{
            path = '';
            for(i = 0; i < filePath.length ; i++) {                                                                
                path += escape(filePath[i].innerHTML) + '/';
            }                                        
            imageName = escape(fileName[0].innerHTML);
            imagePath = path + imageName;
            CKEDITOR.instances['<?php echo ArticleController::MODEL_CLASS_NAME.'[fulltext]'?>'].insertHtml('<img src="'+ '<?php echo $serverUrl;?>'+ imagePath + '"'+ 'alt="' + imageName + '">');
            $('#fileDialog').dialog('close');  
        }
        
    });
            
});        
</script>


